package com.cbgolf.oa.util;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cbgolf.oa.app.App;
import com.cbgolf.oa.entity.NetResponse;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.user.User;
import com.cbgolf.oa.user.UserData;
import java.util.List;
import java.util.Map;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;


/**
 * Created by lq on 2017/10/10.
 */

public class DataUtil {

  /**
   * ali
   */
//  创博OA球场端ANDROID
//  appKey：
//      24846702
//  packageName：
//  com.cbgolf.oa
//  AppSecret：
//      3f56fecbef9ba909974f8cd564417d0d
  /**
   * ali test
   */
//  appKey：
//      24851343
//  packageName：
//  com.cbgolf.oa.test
//  AppSecret：
//      03e89102b4c6f7fc1ba919b2b8ca1d5c
  public static final int INITDATA = 1;
  public static final int REFRESH = INITDATA + 1;
  public static final int LOADMORE = REFRESH + 1;
  public static final int COUNT = 15;
  public static final String CITY = "成都";
  public static final String VERSION_TYPE = "3";
  public static final String KEY_DATA = "DATA";
  //
  public static final String ANDROID = "ANDROID";
  public static final float SERVICE_MONEY = 1f;


  public static String getToken() {
    if (Util.isEquals(WebAPI.IP, WebAPI.TEST_IP)) {
      Log.e("token-------------",
          (UserData.getUser() == null) ? "is null" : UserData.getUser().getToken());
    }
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getToken());
  }
  public static void quitUser() {
    if (null != UserData.getUser()) {
      DbManager db = x.getDb(App.getDaoConfig());
      try {
        db.delete(User.class);
        db.dropTable(User.class);
        UserData.setUser(null);
      } catch (DbException e) {
        e.printStackTrace();
      }

    }
  }


  public static String getUserId() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getId());
  }


  public static String getUserAccount() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getUserAccount());
  }

  public static String getCourseName() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getCourseName());
  }

  public static String getParkId() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getCourseId());
  }

  public static String getGender() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getGender());
  }

  public static boolean hasClosePermission() {
    return UserData.getUser() != null && UserData.getUser().getHaveClosePermission();
  }

  public static String getUserPhone() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getPhone());
  }

  public static String getUserName() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getName());
  }

  public static String getRoles() {
    return UserData.getUser() == null ? "" : TextUtil.Text(UserData.getUser().getRoles());
  }

  public static String getLoginPark() {
    return (null == UserData.getLoginUser() ? ""
        : TextUtil.Text(UserData.getLoginUser().getCourseName()));
  }

  public static String getLoginAccount() {
    return (null == UserData.getLoginUser() ? ""
        : TextUtil.Text(UserData.getLoginUser().getLoginAccount()));
  }

  public static String getLoginAccountStart() {
    return (null == UserData.getLoginUser() ? ""
        : TextUtil.Text(UserData.getLoginUser().getAccountStart()));
  }

  public static String getLoginAccountChild() {
    return (null == UserData.getLoginUser() ? ""
        : TextUtil.Text(UserData.getLoginUser().getAccountChild()));
  }

  public static boolean isHasTeetimePermission() {
    return (null != UserData.getUser() && UserData.getUser().isHasPermission());
  }

  public static void setHasPermission(boolean has) {
    User user = UserData.getUser();
    if (null != user) {
      user.setHasPermission(has);
      UserData.upDataUser(user);
    }
  }

  public static String getJsonString(JSONObject json, String key) {
    return (null != json && json.containsKey(key)) ? json.getString(key) : "";
  }

  public static String getJsonString(String res, String key) {
    JSONObject json = getJson(res);
    return (null != json && json.containsKey(key)) ? json.getString(key) : "";
  }

  public static JSONObject getJson(String res) {
    try {
      return JSONObject.parseObject(res);
    } catch (Exception e) {
      return null;
    }
  }

  public static boolean isContainsJsonkey(JSONObject json, String key) {
    if (Util.isNull(key)) {
      return false;
    }
    return null != json && json.containsKey(key);
  }

  public static boolean isContainsJsonkey(JSONObject json, String key, String key2) {
    if (Util.isNull(key) || Util.isNull(key2)) {
      return false;
    }
    return null != json && json.containsKey(key) && json.containsKey(key2);
  }

  public static int getJsonInt(JSONObject json, String key, int defaul) {
    return (null != json && json.containsKey(key)) ? json.getIntValue(key) : defaul;
  }

  public static List<? extends Object> getJsonArray(String res, Class clazz) {
    try {
      return JSON.parseArray(res, clazz);
    } catch (Exception e) {

    }
    return null;
  }

  public static <T> List<T> getListArray(String res, Class<T> clazz) {
    try {
      return JSON.parseArray(res, clazz);
    } catch (Exception e) {

    }
    return null;
  }

  public static boolean getJsonBoolean(JSONObject json, String key) {
    try {
      return (null != json && json.containsKey(key) && null != json.getBoolean(key))
          ? json.getBoolean(key) : false;
    } catch (Exception e) {

    }
    return false;
  }

  public static String getStringExtra(Intent intent, String key) {
    return (null != intent && intent.hasExtra(key)) ? intent.getStringExtra(key) : "";
  }

  public static boolean getBooleanExtra(Intent intent, String key, boolean defaul) {
    return (null != intent && intent.hasExtra(key)) ? intent.getBooleanExtra(key, defaul) : defaul;
  }

  public static int getIntExtra(Intent intent, String key, int defaul) {
    return (null != intent && intent.hasExtra(key)) ? intent.getIntExtra(key, defaul) : defaul;
  }

  public static float getFloatExtra(Intent intent, String key, float defaul) {
    return (null != intent && intent.hasExtra(key)) ? intent.getFloatExtra(key, defaul) : defaul;
  }

  public static long getLongExtra(Intent intent, String key, long defaul) {
    return (null != intent && intent.hasExtra(key)) ? intent.getLongExtra(key, defaul) : defaul;
  }

//  public static Serializable getSerializableExtra(Intent intent, String key) {
//    return (null != intent && intent.hasExtra(key)) ? intent.getSerializableExtra(key) : null;
//  }

  public static <T> T getSerializableExtra(Intent intent, String key) {
    return (null != intent && intent.hasExtra(key)) ? (T) intent.getSerializableExtra(key) : null;
  }


  public static Object getArrayData(Object[] objs, int index) {
    return (arrayIsNull(objs) || index >= objs.length) ? null : objs[index];
  }

  public static String getArrayString(String[] objs, int index) {
    return (arrayIsNull(objs) || index >= objs.length) ? "" : objs[index];
  }

  public static Object getMapData(Map<Object, Object> map, Object key) {
    return (mapIsNull(map) || !map.containsKey(key)) ? null : map.get(key);
  }

  public static boolean getMapBoolean(Map<Integer, Boolean> map, Integer key) {
    return (null != map && !map.isEmpty() && map.containsKey(key)) ? (null != map.get(key) ? map
        .get(key) : false) : false;
  }

  public static boolean getBundleBoolean(Bundle bundle, String key, boolean defaul) {
    return (null != bundle && bundle.containsKey(key)) ? bundle.getBoolean(key, defaul) : defaul;
  }

  public static String getBundleString(Bundle bundle, String key) {
    return (null != bundle && bundle.containsKey(key)) ? bundle.getString(key) : "";
  }

  public static float getBundleFloat(Bundle bundle, String key, float defaul) {
    return (null != bundle && bundle.containsKey(key)) ? bundle.getFloat(key, defaul) : defaul;
  }

  public static boolean arrayIsNull(Object[] objs) {
    return (null == objs || objs.length == 0);
  }

  public static boolean mapIsNull(Map<? extends Object, ? extends Object> map) {
    return (null == map || map.isEmpty() || map.size() == 0);
  }

  public static boolean isArray(Object obj) {
    return (obj != null && obj.getClass().isArray());
  }

  public static boolean listIsNull(List<? extends Object> list) {
    return null == list || list.size() == 0;
  }

  public static int getBundleInt(Bundle bundle, String key, int defaul) {
    return (null != bundle && bundle.containsKey(key)) ? bundle.getInt(key) : defaul;
  }

  public static boolean isGoodJson(String res) {
    try {
      JSONObject json = JSON.parseObject(res);
    } catch (Exception e) {
      try {
        List<Object> list = JSON.parseArray(res, Object.class);
      } catch (Exception e1) {
        return false;
      }
    }
    return true;
  }

  public static <T> List<T> getList(NetResponse res, Class<T> clazz) {
    try {
      JSONObject json = getJson(res.result);
      String result = (null != json && isContainsJsonkey(json, "content") ?
          getJsonString(json, "content")
          : res.result);
      return JSON.parseArray(result, clazz);
    } catch (Exception e) {
      return null;
    }
  }

//  public static <T> List<T> getList(String result, Class<T> clazz) {
//    try {
//      return JSON.parseArray(result, clazz);
//    } catch (Exception e) {
//      return null;
//    }
//  }

  public static <T> List<T> getList(String res, Class<T> clazz) {
    try {
      String result;
      try {
        JSONObject json = JSON.parseObject(res);
        result = (
            json.containsKey("content") ?
                DataUtil.getJsonString(json, "content")
                : (
                    json.containsKey("status") && json.containsKey("data")
                        && json.containsKey("message") ?
                        json.getString("data") : res));
      } catch (Exception e) {
        result = res;
      }
      if (Util.isNull(result)) {
        return null;
      }
      return JSON.parseArray(result, clazz);
    } catch (Exception e) {
      Log.e("catch------------", e.toString() + "");
      return null;
    }
  }

  public static Object getBean(String res, Class c) {
    try {
      return JSON.parseObject(res, c);
    } catch (Exception e) {
      return null;
    }
  }



}
